package com.sms.mobiles;

import java.io.BufferedReader;
import java.util.Random;

import com.jasson.im.api.APIClient;
import com.jasson.im.api.MOItem;
import com.jasson.im.api.RPTItem;
/**
 * ApiTestDemo
 */
public class ApiTestDemo
{
	private String cmd = "11";
	private String mobileStr = "13516871762,13800138888";
	private String content = "���ŷ��Ͳ������� via  IM JAVA API ";
	private long smId = 10;
	private int smType = 0;
	private String url = "";
	private String host = "218.200.69.63";
	private String dbName = "mas";
	private String apiId = "szapi";
	private String name = "szapi";
	private String pwd = "szapi";
	private APIClient handler = new APIClient();
	BufferedReader in = null;
	public static ApiTestDemo instance;
	public ApiTestDemo(String[] args)
	{
		init();
	}

	public void init()
	{
		int connectRe = handler.init(host, name, pwd, apiId,dbName);
        if(connectRe == APIClient.IMAPI_SUCC)
        	info("��ʼ���ɹ�");
        else if(connectRe == APIClient.IMAPI_CONN_ERR)
        	info("����ʧ��");
        else if(connectRe == APIClient.IMAPI_API_ERR)
        	info("apiID������");
        if(connectRe != APIClient.IMAPI_SUCC)
        {
        	System.exit(-1);
        }
	}
	
	public void release()
	{
		handler.release();
		Thread.currentThread().interrupt();
	}
	
	public void testSend()
	{
		SendTask task = new SendTask();
		task.start();
	}
	
	/**
	 * 
	 * ���ͽӿ�
	 * @param msg
	 */
	public void sendSM(AbstractMessage msg)
	{
		String tmpMobile = null;
		String tmpContent = null;
		String tmpTypeStr = null;
		String tmpSrcID = null ;
		int tmpType = 0;
		String tmpUrl = null;
		
		int result = 0;
		if(msg !=null && msg.isWapPush())
		{
			MessagePush msg1 = (MessagePush)msg;
			result = handler.sendSM(msg1.tmpMobile, msg1.tmpContent, smId, Long.parseLong(msg1.tmpSrcID), msg1.Url);
		}else{
			Message  msg1 = (Message)msg;
			result = handler.sendSM(msg1.tmpMobile, msg1.tmpContent, smId , Long.parseLong(msg1.tmpSrcID));
		}        
        
        if(result == APIClient.IMAPI_SUCC)
        {            
            info("���ͳɹ�\n");
        }
        else if(result == APIClient.IMAPI_INIT_ERR)
            info("δ��ʼ��");
        else if(result == APIClient.IMAPI_CONN_ERR)
            info("��ݿ�����ʧ��");
        else if(result == APIClient.IMAPI_DATA_ERR)
            info("�������");
        else if(result == APIClient.IMAPI_DATA_TOOLONG)
            info("��Ϣ����̫��");
        else if(result == APIClient.IMAPI_INS_ERR)
            info("��ݿ�������");
        else
            info("�����������");
	}
	
	
	/**
	 * 
	 * ��������
	 */
	public MOItem[]  recvSM()
	{
		MOItem[] mos = handler.receiveSM();
		
		//���´������ע�͵� ����ת�Ƶ����?����
        int len = 0, i = 0;
        StringBuffer sb = new StringBuffer("");
        if(mos == null)
        {
            info("δ��ʼ�������ʧ��");
            return null;
        }
        else if(mos.length == 0)
        {
            info("û��MO����");
        }
        else
        {
            len = mos.length;
            while(i < len)
            {
                sb.append("�ֻ����: ");
                sb.append(mos[i].getMobile() + " ");
                sb.append("��������: ");
                sb.append(mos[i].getContent());
                sb.append("MOʱ��: ");
                sb.append(mos[i].getMoTime());
                sb.append("\n");
                i++;
            }
             
            info(sb.toString() );
        }
        return mos;
	}
	
	
	public RPTItem[] recvRPT()
	{
		RPTItem[] rpts = handler.receiveRPT();
		
		//���´������ע�͵� ����ת�Ƶ����?����
        int len = 0, i = 0;
        StringBuffer sb = new StringBuffer("");
        if(rpts == null)
        {
            info("δ��ʼ�������ʧ��");
            return null;
        }
        else if(rpts.length == 0)
        {
            info("û�л�ִ");
        }
        else
        {
            len = rpts.length;
            while(i < len)
            {
                sb.append("�ֻ�: ");
                sb.append(rpts[i].getMobile() + " ");
                sb.append("��ִ����: ");
                sb.append(rpts[i].getCode() + " ");
                sb.append("��ִ����: ");
                sb.append(rpts[i].getDesc() + " ");
                sb.append("��ִʱ��: ");
                sb.append(rpts[i].getRptTime() + " ");
                sb.append("\n");
                i++;
            }
            info(sb.toString());
        }
        return rpts;
	}
	
	public void error(Object obj , Throwable thr)
	{
		info(obj);
		thr.printStackTrace();
	}
	
	public void info(Object obj)
	{
		System.out.println(obj);
	}

	public int getInt(String str)
	{
		int ret = Integer.MIN_VALUE;
		try
		{
			ret = Integer.parseInt(str);
		}catch(NumberFormatException e)
		{
			ret = Integer.MIN_VALUE;
		}
		return ret;
	}
	
	public void quit()
	{
		release();
		System.exit(0);
	}
	
//	public void parseCmd(String[] args)
//	{
//		String tmp = "";
//		int index = 0;
//		int len = args.length;
//		if(args.length > 0)
//		{
//			info("parse argements....");
//			while(index < len)
//			{
//				tmp = args[index++].trim();
//				if(tmp.equalsIgnoreCase("-h"))
//				{
//					host = args[index++];
//					info(" host  = "+host);
//				}
//				else if(tmp.equalsIgnoreCase("-n"))
//				{
//					name = args[index++];
//					info(" name  = "+name);
//				}
//				else if(tmp.equalsIgnoreCase("-p"))
//				{
//					pwd = args[index++];
//					info(" pwd   = "+pwd);
//				}
//				else if(tmp.equalsIgnoreCase("-i"))
//				{
//					apiId = args[index++];
//					info(" apiId = "+apiId);
//				}
//				else
//				{
//					index += 2;
//					continue;
//				}
//			}
//		}
//	}
	
	class SendTask extends Thread
	{
		int loop = 10;
		long interval = 2000L;
		public SendTask()
		{
			
		}
		public void run()
		{
			Random random = new Random();
			long tmpSmId = 0;
			long tmpSrcId = 0;
			while(loop > 0)
			{
				try
				{
					Thread.sleep(interval);
				}catch(InterruptedException e)
				{
				}
				
				tmpSmId = random.nextInt(1000);
				tmpSrcId = random.nextInt(100);
				System.out.println("TestSend:"+loop + "[smId:"+tmpSmId+";srcId:"+tmpSrcId+"]");
				handler.sendSM(mobileStr.split(","), content,  tmpSmId, tmpSrcId);				
				loop--;          
			}
			System.out.println("Test completed.");
		}
	}
	
	public static void main(String[] args)
	{
		CMobileClient client = new CMobileClient();
		client.init();
	}

	
	
	
}
